package com.darwin.darwinBitmapEngine.core
{
	import com.darwin.darwinBitmapEngine.events.NodeEvent;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	/**
	 * 基于SpriteSheet位图阵列的动画的对象 
	 * @author lipi,Roland
	 */
	public class MovieNode extends Node
	{
		/**
		 * 该动画是否循环播放 
		 */		
		public var isLoop:Boolean = true;
		
		private var _timer:Timer;
		/**
		 * 
		 * @param bitmapData 动画的源位图
		 * @param frameRect 每帧动画的矩形，表示当前对象的大小，如果不设定frameList属性，则动画播放以frameRect偏移
		 * @param frameList 帧列表，如果帧矩形非规则矩形则将每帧的矩形放在frameList中
		 */
		public function MovieNode(bitmapData:BitmapData = null,frameRect:Rectangle = null,frameList:Array = null)
		{
			if(bitmapData != null && frameRect != null)
			{
				initMovieNode(bitmapData,frameRect,frameList); 
			}
		}
		
		/**
		 * 初始化movieNode对象，对于需要延迟实例化的对象，构造函数执行时可能还未取得相应的数据，
		 * 可通过initMovieNode完成初始化
		 */
		public function initMovieNode(bitmapData:BitmapData,frameRect:Rectangle,frameList:Array = null):void
		{
			this.bitmapData = bitmapData;
			this._frameList = frameList;
			this.rect = frameRect;
			_currentFrame = 0;
			if(frameList != null)
			{
				_totalFrames = frameList.length;
			}else{
				_totalFrames = int(bitmapData.rect.width/frameRect.width);
			}
			_frameInterval = 1000/frameRate;
			_timer = new Timer(_frameInterval);
			_timer.addEventListener(TimerEvent.TIMER,timerEventComplete);
			_timer.start()
		}
		
		protected function timerEventComplete(event:TimerEvent):void
		{
			if(_isStop == false)
			{
				markChange();
			}
		}
		
		//总帧数 
		private var _totalFrames:int;
		
		/** 
		 * 返回总帧数 
		 */
		public function get totalFrames():int
		{
			return _totalFrames;
		}
		
		//当前帧
		private var _currentFrame:int = 0;
		
		/**
		 * @return 返回当前帧
		 */
		public function get currentFrame():int
		{
			return _currentFrame;
		}
		
		
		//是否处于暂停状态 
		private var _isStop:Boolean = false;
		
		/**
		 * 当前播放是否停止
		 */
		public function get isStop():Boolean
		{
			return _isStop;
		}
		
		/**
		 * 停止动画播放
		 */
		public function stop():void
		{
			_isStop = true;
		}
		
		/**
		 * 开始播放动画 
		 */
		public function play():void
		{
			_isStop = false;
		}
		
		
		/**
		 * 跳某一帧并播放
		 * @param frame 帧索引 
		 */
		public function gotoAndPlay(frame:int):void
		{
			if(frame >= totalFrames)
			{
				_currentFrame = totalFrames - 1;
			}else{
				_currentFrame = frame;
			}
			_isStop = false;
		}
		
		/**
		 * 停止到某一帧 
		 * @param frame 帧索引
		 */
		public function gotoAndStop(frame:int):void
		{
			if(frame >= totalFrames)
			{
				_currentFrame = totalFrames - 1;
			}else{
				_currentFrame = frame;
			}
			_isStop = true;
		}
		
		/**
		 * 将播放头转到下一帧并停止
		 */
		public function nextFrame():void
		{
			if(_currentFrame+1 >= totalFrames)
			{
				_currentFrame = totalFrames - 1;
			}else{
				_currentFrame += 1;
			}
			_isStop = true;
		}
		
		/**
		 * 将播放头转到上一帧并停止
		 */
		public function prevFrame():void
		{
			if(_currentFrame-1 < 0)
			{
				_currentFrame = 0;
			}else{
				_currentFrame -= 1;
			}
			_isStop = true;
		}
		
		
		
		//帧列表数组 
		private var _frameList:Array;
		
		/**
		 * 返回帧列表数组
		 */
		public function get frameList():Array
		{
			return _frameList;
		}
		
		/**
		 * 设置帧列表数组
		 */
		public function set frameList(value:Array):void
		{
			_frameList = value;
		}
		
		
		//两帧间隔时间 
		private var _frameInterval:int;
		
		/**
		 * 返回两帧间隔时间
		 */
		protected function get frameInterval():int
		{
			return _frameInterval;
		}
		
		//帧频  
		private var _frameRate:int = 24;
		
		//上一次执行渲染时间 
		private var _preTime:int = 0;

		/**
		 * 上一次渲染时间
		 */
		protected function get preTime():int
		{
			return _preTime;
		}

		protected function set preTime(value:int):void
		{
			_preTime = value;
		}
		
		private var _renderFrame:int = -1;

		/** 
		 * 渲染处理 
		 */
		override public function render():void
		{
			var $currentTime:int = getTimer();
			if($currentTime - preTime > frameInterval)
			{
				preTime = $currentTime;
				if(this.rect != null)
				{
					if(_renderFrame == currentFrame && !isStop)
					{
						_currentFrame++;
						if(_currentFrame >= totalFrames)
						{
							if(!isLoop)
							{
								_isStop = true;
								var nodeEvent:NodeEvent = new NodeEvent(NodeEvent.MOVIE_COMPLETE);
								dispatchEvent(nodeEvent);
							}else
							{
								_currentFrame = 0;
							}
						}
					}
					this.rect.x = this.rect.width * _currentFrame;
					_renderFrame = currentFrame;
				}
				
			}
		}
		

		/**
		 * 帧速率 
		 */
		public function get frameRate():int
		{
			return _frameRate;
		}

		/**
		 * @private
		 */
		public function set frameRate(value:int):void
		{
			_frameRate = value;
			_frameInterval = 1000/frameRate;
			if(_timer) _timer.delay = _frameInterval;
		}

		
		
		
	}
}